-- FUNCTION: public.widget_PieChart_Pharmacy_LowSellingProducts(date, text, integer)

-- DROP FUNCTION IF EXISTS public."widget_PieChart_Pharmacy_LowSellingProducts"(date, text, integer);

CREATE OR REPLACE FUNCTION public."widget_PieChart_Pharmacy_LowSellingProducts"(
	"fromDate" date,
	"filterType" text,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Date" date, "Name" text, "Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
declare
--f record;
--filtervalue interval;
--intervaldata interval;

f record;
intervaldata interval;
ftype text;
currentdate date;
fromdate date;
begin
intervaldata:= case when "filterType"='day' then '1 D' when "filterType"='month' then '1 Month' when "filterType"='year' then '1 Year' end;
ftype:= case when "filterType"='day' then 'month'::text  when "filterType"='month' then 'year'::text when "filterType"='year' then 'year'::text end;
currentdate:= case when  "filterType"='year' then date_trunc(ftype, "fromDate")  - '2 Y'::interval else date_trunc(ftype, "fromDate") end ;
fromdate:=case when "filterType"='day' and  current_date > "fromDate"  then   (date_trunc('month', "fromDate"::date) + interval '1 month' - interval '1 day')::date
 when  "filterType"='month' and   "fromDate" < current_date then  date_trunc('Year', "fromDate"::date) + interval '1 Year' - interval '1 month' 
 
else "fromDate" end;

 raise notice 'intervaldata %', intervaldata;
 raise notice 'ftype %', ftype;
  raise notice 'currentdate %', currentdate;
create  Temp TABLE temp_table 
(
	startdate date,
   	enddate date, 
	dname text,
  	countdata numeric
 ) ON COMMIT DELETE ROWS;
 
create  Temp TABLE temp_date_table 
( startdate date,
   enddate date
 ) ON COMMIT DELETE ROWS;
 for f in 
 select   generate_series( currentdate  , fromdate,  intervaldata)  weeks 
    loop 
           insert into temp_date_table (startdate,enddate)
		   select f.weeks::text::date ,(f.weeks+ intervaldata)::text::date ;
		   --raise notice 'datetable %', f.weeks::text::date;
   end loop;
for f in 
 select   generate_series( currentdate  , fromdate,  intervaldata)  weeks 
    loop 
	raise notice 'currentdate %', f.weeks::text::date;
	raise notice 'fromdate %', fromdate::date;
	insert into temp_table (startdate,enddate,dname,countdata)
----------------------------------------------
with lowSellingData as(
select psd."PharmacyProductId",PP."ProductName",  count(psd."PharmacyProductId") "Count" 
from "PharmacySaleDetail" psd
	
	join "PharmacyProduct" pp on pp."PharmacyProductId" = psd."PharmacyProductId"	
	join "PharmacySaleHeader" psh on psh."PharmacySaleHeaderId" = psd."PharmacySaleHeaderId"
	where   psh."CreatedDate"::date >= f.weeks::text::date and psh."CreatedDate"::date <= (f.weeks+ intervaldata)::text::date  
	and case when ("locationId" is null) or ("locationId"=0) then 1=1 else psh."LocationId"= "locationId" end
	group by psd."PharmacyProductId" ,PP."ProductName" 
	
)
,perlowSellingData as (
select a.weeks,a."ProductName",round(a."Count"/sum(a."Count") over()*100,2) "Count" from(
select f.weeks, lsd."ProductName", lsd."Count"
from lowSellingData lsd)a)

select date_trunc('month', current_date)::date startdate ,f.weeks enddate,lsd."ProductName" , lsd."Count"
from perlowSellingData lsd;
---------------------
	
 
----------------------------------------------

end loop;
	RETURN QUERY
	with perlowSellingData as (select A.endDate,A.dname,sum(coalesce(A.countdata,0) )::numeric countdata from temp_table A group by A.endDate,A.dname)
	
	select A.startdate,B.dname,coalesce(B.countdata,0) "Count" from perlowSellingData B 
	left join temp_date_table A  on A.startdate=B.endDate
	order by "Count" asc
	limit 10;
	
	drop table temp_table;
	drop table temp_date_table;				
END;
$BODY$;

ALTER FUNCTION public."widget_PieChart_Pharmacy_LowSellingProducts"(date, text, integer)
    OWNER TO postgres;
